;(function(window){
	var g_width;
	function set(){
		var style = document.createElement('style'),
			width = document.documentElement.offsetWidth,
			fixed = width >= 500 ? 20 : 15;
		if(g_width == width){
			return;
		}
		g_width = width;
		if(width >=640){
			width = 640;
		}
		//style.innerText = '.pic_focus { height:'+ parseInt(width*320/640) +'px} .pic_info { height:'+ parseInt( (width-fixed)*560/600 ) +'px}';
		document.querySelector('head').appendChild(style);
	}
	Event.add(window,'resize',set);
	set();
})(window);



/**
 * 滚动图
 * 
 ***/
;(function(window){
	function scroll(){
	
		var box,
			obj,
			tags,
			c,
			width,
			height,
			preBtn,
			anima,
			nextBtn,
			state,
			callback,
			start,
			end,
			range,
			current,
			parent,
			clickFn,
			resizeFn,
			old_style,
			cb,
			len;
		
		
		
		function init(opt){
			box = document.querySelector(opt.box);
			
			tags = box.querySelectorAll(opt.tag);
			parent = opt.parent;
			preBtn = document.querySelector(opt.preBtn);
			nextBtn = document.querySelector(opt.nextBtn);
			len = tags.length;
			cb = opt.callback || null;
			if(len==1) return;
			c = opt.current ||  0;
			old_style = 'left 0.3s ease-in';
			
			getSize();
			box.style.cssText = 'width:' + len*width + 'px;height:' + height +'px;';
			
			
			range = [0,-width*(len-1)];
			clickFn = opt.click || null;
			resizeFn = opt.resize || null;
			
			
			
			
			
			set();
			hander();
			
			
			
		}
		function getSize(){
			
			if(parent == 'document'){
				width = document.documentElement.clientWidth;
				height = document.documentElement.clientHeight;
			}else{
				var temp = document.querySelector(parent);
				width = temp.offsetWidth;
				height = temp.offsetHeight;
			}
		}
		function set(){
			for(var i=0;i<len;i++){
				tags[i].style.cssText += 'left:'+ width*i +'px;width:'+ width +'px;height:'+ height + 'px;';
			}
		}
		function hander(){
			if(preBtn){
				Event.add(preBtn,'click',function(){
					change()
				})
			}
			if(nextBtn){
				Event.add(nextBtn,'click',function(){
					change(true);
				})
			}
			Event.add(window,'resize',function(){
				if(state){
					callback = function(){
						reset();
					}
					return;
				}
				callback = null;
				reset();
			})
			
			
			var path ;
			var timer = null;
			var to;
			function handerEvent(e){
				switch(e.type){
					case "touchstart" : 
						startFn(e);
						break;
					case 'touchmove' : 
						moveFn(e);
						break;
					case 'touchend' : 
						setTimeout(endFn(e),0);
						break;
				}
			}
			function startFn(e){
				path = false;
				clearTimeout(timer);
				timer = null;
				current = null;
				if(state) {
					clear();
					return;
				}
				
				if(e.targetTouches.length == 1){
					
					var touch = e.targetTouches[0];
					start = {
						page : touch.pageX,
						y : touch.pageY,
						left : parseInt(Element.getStyle(box,'left'))
					}
					Event.add(box,'touchmove',handerEvent);
					Event.add(box,'touchend',handerEvent);
				}
				
			}
			function moveFn(e){
				if(state) {
					clear();
					return;
				}
				
				if(e.targetTouches.length == 1){
					var touch = e.targetTouches[0];
					current = {
						page : touch.pageX,
						y : touch.pageY
					}
					
					var x = Math.abs(current.page - start.page),
						y = Math.abs(current.y - start.y);
					
					if( x <= 30 ||  y >= 70   ){
						path = 'scroll';
					}else if( x <=10 &&  y <= 10 ){
						path = 'tap';
					}else{
						path = 'swipe';
					}
					
					if(path == 'swipe'){
						e.preventDefault();
						//e.stopPropagation();
						
						to = start.left +  (current.page - start.page);
						if(to>= range[0]){
							to = range[0]
						}
						if(to<= range[1]){
							to = range[1]
						}
						box.style.cssText += ';left:'+ to +'px;transition:0;';
						return;
					}
				}
			}
			
			
			function endFn(e){
				if(to%width !=0){
					to = Math.round(to/width) * width;
				}
				box.style.cssText += ';left:'+ to +'px;transition:0.3s;';
				
				if(!current){
					path = 'tap';
				}
				if(state) {
					clear();
					path = false;
					return;
				}
				
				if(path == 'tap'){
					if(clickFn){
						clickFn(c);
					}
				}
				if(path == 'swipe'){
					state = false;
					moveEnd();
				}
				path = false;
				current = start = null;
				state = false;
				Event.remove(box,'touchmove',handerEvent);
				Event.remove(box,'touchend',handerEvent);
			}
			
			
			Event.add(box,'touchstart',handerEvent);
			
		}
	
		function moveEnd(){
			
			var n = current.page - start.page > 0 ? -1 : 1;
			state = true;
			
			c += n;
			if(c>=len){
				c = len-1;
				state = false;
				return;
			}
			if(c<0){
				c=0;
				state = false;
				return;
			}
			
			box.style.left = -c*width + 'px';
			if(cb){
				cb(c);
			}
			setTimeout(function(){
				state = false;
			},100)
			
			start = current = null;
		}
		function clear(){
			start = null;
			end = null;
			current = null;
		}
		function reset(w){
			if(resizeFn){
				resizeFn();
			}
			getSize();
			box.style.cssText += ';width:'+ len*width +'px;height:'+ height +'px;left:'+ -c*width +'px;';
			set();
		}
		function setWidth(w){
			width = w || width;
		}
		function setCurrent(n){
			c = n;
			getSize();
			box.style.left = -c*width + 'px';
		}
		function getCurrent(){
			return c;
		}
		function change(type){
			if(type){
				c++;
			}else{
				c--;
			}
			if(c<0){
				c = 0;
				return;
			}
			if(c>= len){
				c = len-1;
				return;
			}
			state = true;
			box.style.left = -c*width + 'px';
			state = false;
			if(cb) cb(c);
			if(callback) callback();
		}
		
		return {
			'init' : init,
			'setCurrent' : setCurrent,
			'getCurrent' : getCurrent,
			'setWidth' : setWidth
		}
	
	}
	
	window['ScrollImg'] = scroll
})(window);



/**
 *  延时加载图片
 *  **/
;(function(window){
	var img;
	function get(o){
		if(!o) return;
		img = o.querySelectorAll('img');
		for(var i=0,l=img.length;i<l;i++){
			set(i);
		}
	}
	function set(n){
		if(img[n].getAttribute('_src')){
			img[n].src = img[n].getAttribute('_src');
			img[n].removeAttribute('_src');
		}
	}
	window['LoadImg'] = get;
})(window);


/**
 *	图集
 *  **/
;(function(window){
	var box,
		tags,
		c,
		width,
		preBtn,
		anima,
		nextBtn,
		state,
		callback,
		start,
		end,
		range,
		current,
		cb,
		len;
	
	function init(opt){
		box = document.querySelector(opt.box);
		tags = box.querySelectorAll(opt.tag);
		preBtn = document.querySelector(opt.preBtn);
		nextBtn = document.querySelector(opt.nextBtn);
		len = tags.length;
		cb = opt.callback || null;
		if(len==1) return;
		c = 0;
		width = box.parentNode.offsetWidth;
		box.style.width = len*width + 'px';
		range = [0,-width*(len-1)];
		
		anima = Anima(box);
		set();
		hander();
		
	}
	function set(){
		for(var i=0;i<len;i++){
			tags[i].style.cssText = 'left:'+ width*i +'px;width:'+ width +'px;';
		}
	}
	function hander(){
		if(preBtn){
			Event.add(preBtn,'click',function(){
				change()
			})
		}
		if(nextBtn){
			Event.add(nextBtn,'click',function(){
				change(true);
			})
		}
		Event.add(box,'click',function(){
			fullScreen.show(c);
		})
		Event.add(window,'resize',function(){
			if(state){
				callback = function(){
					reset();
				}
				return;
			}
			callback = null;
			reset();
		})
	}
	function clear(){
		start = null;
		end = null;
		current = null;
	}
	function reset(){
		width = box.parentNode.offsetWidth;
		box.style.cssText = 'left:'+ -c*width +'px;width:'+ len*width +'px;';
		range = [0,-width*(len-1)];
		set();
	}
	function change(type){
		if(type){
			c++;
		}else{
			c--;
		}
		if(c<0){
			c = 0;
			return;
		}
		
		if(c>= len){
			c = len-1;
			return;
		}
		
		state = true;
		box.style.left = -width*c + 'px';
		state = false;
		if(callback) callback();
		if(cb){
			cb(c);
		}
	}
	
	window['PicShow'] = init;
})(window);



/**
 * @author {tiger}
 */
function lazyload(){
	function each(ar,fn){
		for(var i=0,l=ar.length;i<l;i++){
			fn.call(ar[i],i);
		}
	}
	var addEvent = (function(){ 
		return document.attachEvent ? 
			function(obj,type,fn){ 
				obj.attachEvent('on'+type,fn)
			} : 
			function(obj,type,fn){ 
				obj.addEventListener(type,fn,false)
			}
		})()
	var removeEvent = (function(){ 
			return document.attachEvent ? 
				function(obj,type,fn){ 
					obj.detachEvent('on'+type,fn)
				} : 
				function(obj,type,fn){ 
					obj.removeEventListener(type,fn,false)
				}
		})()
	
	var imgs = document.getElementsByTagName('img'),
		body = document.getElementsByTagName('body')[0],
		sHeight = document.documentElement.clientHeight || document.body.clientHeight,
		sWidth = document.documentElement.clientWidth || document.body.clientWidth,
		overflow = [ sWidth , sHeight ] ,
		range ={
			x:[0,overflow[0]],
			y:[0,overflow[1]]
		};
	
	function filter(){
		if(imgs.length ==0){
			removeEvent(window,'scroll',filter);
			return;
		}
		var _ar = [],
			site = {
				x:document.body.scrollLeft || document.documentElement.scrollLeft,
				y:document.body.scrollTop || document.documentElement.scrollTop
			},
			range = {
				x:[site.x-overflow[0]/2 , site.x+overflow[0]],
				y:[site.y-overflow[1]/2 , site.y+overflow[1]]
			},
			temp = null;
		each(imgs,function(){
			temp = getSite(this);
			//if(temp.x>=range.x[0] && temp.x <= range.x[1] && temp.y >= range.y[0] && temp.y <= range.y[1]){
			if(temp.y >= range.y[0] && temp.y <= range.y[1]){
				this.src = this.getAttribute('_src');
				//this.removeAttribute('_src');
			}else{
				_ar.push(this);
			}
		})
		imgs = [].concat(_ar);
		_ar = null;
		return imgs;
	}
	
	function getSite(obj){
		var s={x:0,y:0};
			while(obj){
				s.x+=obj.offsetLeft;
				s.y+=obj.offsetTop;
				obj=obj.offsetParent;
			}
		return s;
	}
	
	function init(){
		var ar = [];
		each(imgs,function(){
			if(this.getAttribute('_src')){
				ar.push(this)
			}
		})
		imgs = [].concat(ar);
		ar = null;
		filter();
	}
	
	init();
	
	addEvent(window,'scroll',filter);
	addEvent(window,'resize',function(){
		sHeight = document.documentElement.clientHeight || document.body.clientHeight;
		sWidth = document.documentElement.clientWidth || document.body.clientWidth;
		overflow = [ sWidth , sHeight ];
		filter();
	})

}


